﻿// ———————————————————————–
// <copyright file="RouterRuleService.svc.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// RouterRuleService.svc.cs - "Allows Router Rule Service To Interact With The DAL"
// Project: "IC.NETSOA"- "ICNETREST"
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
                
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Xml.Linq;
using ICNETDAL;

namespace ICNETREST
{
  /// <summary>
  /// Contains methods that the RouterRules services uses to modify, delete, of get rules from the database.
  /// </summary>
  [ServiceContract(Namespace = "urn:icnet:mitre:org:RouterRuleService")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  public class RouterRuleService
  {
    /// <summary>
    /// Gets the router rules.
    /// </summary>
    /// <returns>The router rules, formatted as an XML object.</returns>
    [WebGet(UriTemplate = "/", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetRouterRules()
    {
      XElement xe = RouterRulesDAL.GetRouterRules();
      return xe;
    }

    /// <summary>
    /// Gets the role associated with a certain ID.
    /// </summary>
    /// <param name="id">The ID used in looking up the role.</param>
    /// <returns>Thoe role associated with the ID, formatted as an XML object.</returns>
    [WebGet(UriTemplate = "/{Id}", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetRoleByID(string id)
    {
      try
      {
        System.Guid gid = new Guid(id);
        XElement xe = RouterRulesDAL.GetRouterRule(gid);
        return xe;
      }
      catch (FormatException) 
      { 
      }

      return new XElement("RouterRule", null);
    }

    /// <summary>
    /// Creates a new router rule and adds it to the database.
    /// </summary>
    /// <param name="xe">An XML object that will be used to create the rule in XML format.</param>
    /// <returns>The router rule that was added to the database. It will be be noted inside the XML object if any fields were left blank.</returns>
    [WebInvoke(UriTemplate = "/", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement CreateNewRule(XElement xe)
    {
      XElement result = RouterRulesDAL.CreateRouterRule(xe);
      return result; 
    }

    /// <summary>
    /// Modifies a router rule and changes it in the database.
    /// </summary>
    /// <param name="id">The ID of the rule to modify</param>
    /// <returns>The modified router rule that was added to the database. It will be be noted inside the XML object if any fields were left blank.</returns>
    [WebInvoke(UriTemplate = "/{ID}", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement ModifyRule(XElement xe, string id)
    {
        try
        {
            System.Guid gid = System.Guid.Parse(id);
            XElement result= RouterRulesDAL.ModifyRouterRule(xe, gid);
            return result;
        }
        catch (FormatException)
        {
            
        }

        return new XElement("RouterRule", null); 
    }    

    /// <summary>
    /// Deletes a role from the database. 
    /// </summary>
    /// <param name="id">The ID of the role to delete.</param>
    /// <returns>The object that was deleted, as an XML object.</returns>
    [WebInvoke(UriTemplate = "/{Id}", Method = "DELETE", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement DeleteRoleByID(string id)
    {
      try
      {
        System.Guid gid = System.Guid.Parse(id);
        RouterRulesDAL.DeleteRouterRule(gid);
      }
      catch (FormatException) 
      {
      }

      return new XElement("RouterRule", id);
    }
  }   
}
